import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ValidationPipe,
  Res,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Response } from 'express';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('add')
  async create(
    @Body(new ValidationPipe({ whitelist: true }))
    createUserDto: CreateUserDto,
    @Res() res: Response,
  ) {
    const { password, ...result } =
      await this.userService.create(createUserDto);
    // 这里使用了解构赋值，将password从结果中移除。
    res.status(200).send(result);
  }

  @Get()
  findAll() {
    return this.userService.findAll();
  }

  @Post('clear')
  clear(@Res() res: Response) {
    this.userService.clear();
    res.status(200).send({
      status: 0,
      message: '表清除成功!',
      data: [],
    });
  }

  @Get(':name')
  findOne(@Param('name') name: string) {
    return this.userService.findOne(name);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}
